<template>
    <div>
      <el-container style="border: 1px solid #eee">
        <newLeft/>
        <el-container direction="vertical">
          <newHeader/>
          <el-main>
            <el-form :model="ruleForm" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
              <el-form-item label="用户名称" prop="name">
                <el-input v-model="ruleForm.name"></el-input>
              </el-form-item>
              <el-form-item label="出生地区" prop="region">
                <el-select v-model="ruleForm.region" placeholder="请选择地区">
                  <el-option label="上海" value="shanghai"></el-option>
                  <el-option label="北京" value="beijing"></el-option>
                </el-select>
              </el-form-item>
              <el-form-item label="出生时间" required>
                <el-col :span="11">
                  <el-form-item prop="date1">
                    <el-date-picker type="date" placeholder="选择日期" v-model="ruleForm.date1"
                                    style="width: 100%;" value-format="yyyy-MM-dd"></el-date-picker>
                  </el-form-item>
                </el-col>
              </el-form-item>
              <el-form-item label="是否推送" prop="push">
                <el-switch v-model="ruleForm.push"></el-switch>
              </el-form-item>
              <el-form-item label="个人爱好" prop="hobbyNew">
                <el-checkbox-group v-model="ruleForm.hobbyNew">
                  <el-checkbox label="足球" name="type"></el-checkbox>
                  <el-checkbox label="篮球" name="type"></el-checkbox>
                  <el-checkbox label="摄影" name="type"></el-checkbox>
                  <el-checkbox label="听歌" name="type"></el-checkbox>
                </el-checkbox-group>
              </el-form-item>
              <el-form-item label="性别" prop="sex">
                <el-radio-group v-model="ruleForm.sex">
                  <el-radio label="10">男</el-radio>
                  <el-radio label="20">女</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item label="备注" prop="remark">
                <el-input type="textarea" v-model="ruleForm.remark"></el-input>
              </el-form-item>

              <el-form-item>
                <el-button type="primary" @click="submitForm('ruleForm')">立即创建</el-button>
                <el-button @click="resetForm('ruleForm')">重置</el-button>
              </el-form-item>
            </el-form>
          </el-main>
        </el-container>
      </el-container>
    </div>
</template>

<script>
  import newLeft from '@/components/newLeft'
  import newHeader from '@/components/newHeader'
  export default {
    data() {
      return {
        ruleForm: {
          name: '',
          region: '',
          date1: '',
          push: false,
          sex: '',
          remark: '',
          hobbyNew:[],
          hobby:''
        },
        rules: {
          name: [
            { required: true, message: '请输入姓名', trigger: 'blur' },
            { min: 3, max: 5, message: '长度在 3 到 5 个字符', trigger: 'blur' }
          ],
          region: [
            { required: true, message: '请选择出生区域', trigger: 'change' }
          ],
          date1: [
            { required: true, message: '请选择日期', trigger: 'change' }
          ],
          hobby: [
            { type: 'array', required: true, message: '请至少选择一个爱好', trigger: 'change' }
          ],
          sex: [
            { required: true, message: '请选择性别', trigger: 'change' }
          ],
          desc: [
            { required: true, message: '请填写备注', trigger: 'blur' }
          ]
        }
      };
    },
    methods: {
      submitForm(formName) {
        this.$refs[formName].validate((valid) => {
          if (valid) {
            let hob = '';
            for (let i = 0; i< this.ruleForm.hobbyNew.length; i++) {
              hob = hob+','+this.ruleForm.hobbyNew[i];
            }
            this.ruleForm.hobby = hob;

            let url = 'http://localhost/vue-springboot/user/save';
            this.$axios.post(url,this.ruleForm).then( response => {
              let data = response.data;
              if (data.tag) {
                //this.$router.push({ path : '/main'})
                this.$message({
                  message: '恭喜你，这是一条成功消息',
                  type: 'success'
                });
              } else {
                alert('NO');
              }

            }).catch(error => {
              console.log(error);
            })

          } else {
            console.log('error submit!!');
            return false;
          }
        });
      },
      resetForm(formName) {
        this.$refs[formName].resetFields();
      }
    },
    components:{
      newLeft,
      newHeader
    },

  }
</script>

<style scoped>

</style>
